//
//  EquipmentHistoryViewController.m
//  PPScaleDemo
//
//  Created by 彭思远 on 2021/10/28.
//

#import "EquipmentHistoryViewController.h"
#import "BodyDetailTool.h"

@interface EquipmentHistoryViewController ()<PPDataInterface, PPBleStateInterface>
@property (strong, nonatomic) IBOutlet UITableView *table;
@property (nonatomic, strong) PPScaleManager *scaleManager;
@property (nonatomic, strong) NSMutableArray <PPBodyHistoryBaseModel *>*historyData;
@end

@implementation EquipmentHistoryViewController

+ (instancetype)instance{
    return [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"EquipmentHistoryViewController"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"Equipment History";
    [self newSearch];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.scaleManager disconnect];
}


- (void)newSearch{
    self.historyData = @[].mutableCopy;
    self.scaleManager = [[PPScaleManager alloc] initWithMacAddressList:@[self.deviceModel.deviceMac] andUserModel:nil];
    self.scaleManager.stateInterface = self;
    self.scaleManager.dataInterface = self;
    [self.scaleManager startSearching];

}


- (void)monitorBluetoothWorkState:(PPBleWorkState)state{
    if (state == PPBleWorkStateCouldReciveData) {
        [self.scaleManager fetchHistoryData];
    }
}

- (void)scaleManager:(PPScaleManager *)manager monitorHistorData:(PPBodyHistoryBaseModel *)model{
    
    [self.historyData addObject:model];
    if (model.isEnd) {
        [self.table reloadData];
        [manager deleteHistoryData];
    }
}



#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.historyData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
    }
    PPBodyHistoryBaseModel *model = self.historyData[indexPath.row];
    PPUserModel *user = [[PPUserModel alloc] initWithUserHeight:180 gender:PPUserGenderMale age:18 pregnantMode:NO athleteMode:NO andUnit:PPUnitKG];
    PPDeviceModel *device = [PPDeviceModel fakeDeviceForCalcuate:PPDeviceCalcuteTypeAlternate];
    PPBodyFatModel *fat = [[PPBodyFatModel alloc] initWithUserModel:user deviceModel:device weight:model.weight heartRate:model.heartRate isHeartRateEnd:YES unit:PPUnitKG andImpedance:model.impedance];
    BodyDetailTool *tool = [[BodyDetailTool alloc] initWithPPBodyFatModel:fat];
    NSArray *bodyArr = [tool flagBodyArr];
    NSArray *jsonArr = [BodyDetailModel mj_keyValuesArrayWithObjectArray:bodyArr];
    NSString *jsonStr = [jsonArr mj_JSONString];
    
    cell.textLabel.text = jsonStr;
    cell.textLabel.numberOfLines = 0;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}


@end
